package lookie.app.ui.status;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.font.FontRenderContext;
import java.awt.font.LineMetrics;
import java.awt.geom.Rectangle2D;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class MemoryStatus extends JComponent {

	private static final long serialVersionUID = 4216547382900323429L;

	private static final String memoryTestStr = "999/999Mb";

	private final LineMetrics lm;

	private final Color progressForeground;

	private final Color progressBackground;

	public MemoryStatus() {
		// fucking GTK look and feel
		Font font = new JLabel().getFont();
		// Font font = UIManager.getFont("Label.font");
		MemoryStatus.this.setFont(font);

		FontRenderContext frc = new FontRenderContext(null, false, false);
		Rectangle2D bounds = font.getStringBounds(memoryTestStr, frc);
		Dimension dim = new Dimension((int) bounds.getWidth(), (int) bounds
				.getHeight());
		setPreferredSize(dim);
		setMaximumSize(dim);
		lm = font.getLineMetrics(memoryTestStr, frc);

		setForeground(Color.BLACK);
//		setBackground(Color.BLACK);

		progressForeground = Color.BLACK;
//		progressBackground = new Color(0xFFF8C6);
		progressBackground = Color.getHSBColor(40, 240, 226);

	}

	@Override
	public String getToolTipText() {
		// Runtime runtime = Runtime.getRuntime();
		// long free = runtime.freeMemory();
		// long total = runtime.totalMemory();
		// long used = total - free;
		// args[0] = (int) (used / 1024);
		// args[1] = (int) (total / 1024);
		// return jEdit.getProperty("view.status.memory-tooltip",args);
		return "";
	}

	// {{{ paintComponent() method
	@Override
	public void paintComponent(Graphics g) {
		Insets insets = new Insets(0, 0, 0, 0);// MemoryStatus.this.getBorder().getBorderInsets(this);

		Runtime runtime = Runtime.getRuntime();
		long free = runtime.freeMemory();
		long total = runtime.totalMemory();
		long used = total - free;

		int width = MemoryStatus.this.getWidth() - insets.left - insets.right;
		int height = MemoryStatus.this.getHeight() - insets.top - insets.bottom
				- 1;

		float fraction = ((float) used) / total;

		g.setColor(progressBackground);

		g.fillRect(insets.left, insets.top, (int) (width * fraction), height);

		String str = (used / 1024 / 1024) + "/" + (total / 1024 / 1024) + "Mb";

		FontRenderContext frc = new FontRenderContext(null, false, false);

		Rectangle2D bounds = g.getFont().getStringBounds(str, frc);

		Graphics g2 = g.create();
		g2.setClip(insets.left, insets.top, (int) (width * fraction), height);

		g2.setColor(progressForeground);

		g2.drawString(str, insets.left
				+ ((int) (width - bounds.getWidth()) / 2),
				(int) (insets.top + lm.getAscent()));

		g2.dispose();

		g2 = g.create();

		g2.setClip(insets.left + (int) (width * fraction), insets.top,
				MemoryStatus.this.getWidth() - insets.left
						- (int) (width * fraction), height);

		g2.setColor(MemoryStatus.this.getForeground());

		g2.drawString(str, insets.left
				+ ((int) (width - bounds.getWidth()) >> 1),
				(int) (insets.top + lm.getAscent()));

		g2.dispose();
	}

	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().add(new MemoryStatus(), BorderLayout.CENTER);
		frame.setSize(500, 400);
		frame.setVisible(true);
	}
	
}
